﻿using System.ComponentModel;
using System.Runtime.InteropServices;
using System.Threading;

namespace Eneta.Portal.Forum.Utility
{
    internal class ForumTimer
    {
        // Fields
        private readonly long _freq;
        private long _startTime;
        private long _stopTime;

        // Methods
        public ForumTimer(bool startTimer)
        {
            if (!QueryPerformanceFrequency(out _freq))
            {
                throw new Win32Exception();
            }
            if (startTimer)
            {
                Start();
            }
        }

        [DllImport("Kernel32.dll")]
        private static extern bool QueryPerformanceCounter(out long lpPerformanceCount);
        [DllImport("Kernel32.dll")]
        private static extern bool QueryPerformanceFrequency(out long lpFrequency);
        public void Start()
        {
            Thread.Sleep(0);
            QueryPerformanceCounter(out _startTime);
        }

        public void Stop()
        {
            QueryPerformanceCounter(out _stopTime);
        }

        // Properties
        public double Duration
        {
            get
            {
                return ((_stopTime - _startTime) / ((double)_freq));
            }
        }
    }
}
